package abd.pr1.Mappers;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import abd.pr1.Models.Actor;
import abd.pr1.Models.Personaje;

public class ActoresMapper extends AbstractMapper<Actor, Integer> {

	private static final String[] ACTORES_KEY_COLUMN_NAMES = new String[] { "id" };
	private static final String[] ACTORES_COLUMN_NAMES = new String[] { "id", "foto", "nacimiento", "nombre" };
	private static final String ACTORES_TABLE_NAME = "actores";
	
	public ActoresMapper(DataSource ds) {
		super(ds);
	}
	
	@Override
	protected String getTableName() {
		return ACTORES_TABLE_NAME;
	}

	@Override
	protected String[] getColumnNames() {
		return ACTORES_COLUMN_NAMES;
	}

	@Override
	protected Object[] serializeObject(Actor object) {
		return new Object[] { object.getId(), object.getFoto(), object.getNacimiento(), object.getNombre()};
	}

	@Override
	protected String[] getKeyColumnNames() {
		return ACTORES_KEY_COLUMN_NAMES;
	}

	@Override
	protected Object[] serializeKey(Integer key) {
		return new Object[] { key };
	}

	@Override
	protected Actor buildObject(ResultSet rs) throws SQLException {
		Integer id = rs.getInt("id");
		String nombre = rs.getString("nombre");
		Date nacimiento = rs.getDate("nacimiento");
		Blob foto = rs.getBlob("foto");
		return new Actor(id, nombre, nacimiento, foto.getBytes(1,(int)foto.length()));
	}

	@Override
	protected Integer getKey(Actor object) {
		return object.getId();
	}

	@Override
	protected boolean hasAutoincrementKey() {
		return true;
	}

	@Override
	void updateKeys(ResultSet rs, Actor object) throws SQLException {
		Integer id = rs.getInt(1);
		object.setId(id);
	}
	public List<Actor> buscarNombreActor(String nombre){
		nombre = "%"+nombre+"%";
		QueryCondition condicion = new QueryCondition("nombre", Operator.LIKE, nombre);
		return findByConditions(new QueryCondition[] {condicion});
	}
	
	
}
